﻿<%@ Page Title="" Language="C#" MasterPageFile="~/User/User.Master" ClientIDMode="Static" AutoEventWireup="true" CodeBehind="CGD_Edit.aspx.cs" Inherits="CoolERP_Web.User.CGD_Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="../Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgcore.min.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgdialog.js" type="text/javascript"></script>
<script type="text/javascript">
    function setPrice(obj) {
        var reg = /^[0-9]+(.[0-9]{1,2})?$/;
        if (!reg.test(obj.value)) {
            alert("请输入正确的数字");
            obj.value = "";
        }
        else {
            var price = $(obj).val();
            var count = $(obj).closest("tr").find("[id=txtCount]").val();
            if (isEmpty(count))
                count = 0;
            $(obj).closest("tr").find("td").eq(8).text(parseInt((count) * price).toFixed(2));
            setTotal();
        }
    }
    function setCount(obj) {
        var count = parseInt($(obj).val());
        if (count > 0) {
            var price = $(obj).closest("tr").find("[id=txtPrice]").val();
            if (isEmpty(price))
                price = 0;
            $(obj).closest("tr").find("td").eq(8).text((count * price).toFixed(2));
            setTotal();
        }
        else {
            $(obj).val("");
            alert('数量不能小于1');
        }
    }
    function deleteWL(obj) {
        $(obj).closest("tr").remove();
    }
    var dg;
    function selectWL() {
        if (isEmpty($("#hidGYSID").val())) {
            alert("请选择供应商");
        }
        else {
            dg = new J.dialog({ id: 'select', title: '选择物料', page: 'SelectWLKC.aspx?gid=' + $("#hidGYSID").val(), iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 1000, height: 700 });
            dg.ShowDialog();
        }
    }
    function selGYS() {
        dg = new J.dialog({ id: 'select', title: '选择供应商', page: 'SelectGYS.aspx', iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 700 });
        dg.ShowDialog();
    }
    function selMore() {
        J("#GridView1 input[type=checkbox][id=chkSel]", dg.dgDoc).each(function () {
            if ($(this).attr("checked") == true) {
                var row = $("#tbGoodsInfo tr").length;
                var tr = $(this).closest("tr").clone();
                var id = $(this).val();
                var bh = $(tr).find("[id='hidBH']").val();
                var name = $(tr).find("[id='hidMC']").val();
                var gg = $(tr).find("[id='hidGG']").val();
                var ys = $(tr).find("[id='hidYS']").val();
                var dwmc = $(tr).find("[id='hidDWMC']").val();
                var dwbh = $(tr).find("[id='hidDWBH']").val();
                var kcsl = $(tr).find("[id='hidSL']").val()
                var price = $(tr).find("[id='hidDJ']").val();
                var hasAdd = false;
                $("#tbGoodsInfo input[type=hidden][id='hidWid']").each(function () {
                    hasAdd = $(this).val() == id ? true : false;
                    if (hasAdd)
                        return false;
                });
                if (!hasAdd) {
                    $("<tr><td>" + row + "</td><td>" + bh + "</td><td>" + name + "<input type=hidden id='hidWid' value='" + id + "' /></td>\
                <td>" + gg + "</td><td>" + ys + "</td><td>" + dwmc + "<input type=hidden id='hidDW' value='" + dwbh + "' /></td>\
                <td><input type=text class='textbox' id='txtPrice' value='" + price + "' style='width:60px; text-align:center' setPrice='setPrice(this)' /></td>\
                <td><input type=text class='textbox' id='txtCount' value='' style='width:60px; text-align:center' onchange='setCount(this)' /></td>\
                <td><img src='../Images/Common/btn_del0.png' onclick='deleteWL(this)' style='cursor:pointer' /></td></tr>").appendTo("#tbGoodsInfo");
                }
            }
        });
        dg.cancel();
    }
    function setTotal() {
        var totalMoney = 0;
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var txt_price = $(this).find("[id='txtPrice']").val();
                var txt_count = $(this).find("[id='txtCount']").val();
                if (!isEmpty(txt_price) && !isEmpty(txt_count))
                    totalMoney += parseFloat(txt_price) * parseFloat(txt_count);
            }
        });
        $("#spanMoney").text(totalMoney.toFixed(2));
        if (!isEmpty($("#ddlHB").val()))
            $("#spanDW").text($("#ddlHB").find("option:selected").text());
        $("#divTotalMoney").show();
    }
    function checkSubmit() {
        if (isEmpty($("#hidGYSID").val())) {
            alert("请选择供应商");
            return false;
        }
        if (isEmpty($("#ddlDepartments").val())) {
            alert("请选择采购部门");
            return false;
        }
        if (isEmpty($("#hidStaff").val())) {
            alert("请选择采购人员");
            return false;
        }
        var isOk = true;
        var idList = new Array();
        var priceList = new Array();
        var countList = new Array();
        var ckidList = new Array();
        var cwidList = new Array();
        var dwList = new Array();
        $("#tbGoodsInfo tr").each(function (i, item) {
            if (i > 0) {
                var wl = {
                    id: $(this).find("input[type=hidden][id='hidWid']").val(),
                    price: $(this).find("input[type=text]").eq(0).val(),
                    count: $(this).find("input[type=text]").eq(1).val(),
                    dw: $(this).find("input[type=hidden][id='hidDW']").val()
                };
                if (!isEmpty(wl.id) && isEmpty(wl.price)) {
                    alert("请填写第" + i + "个物料的单价");
                    isOk = false;
                    return isOk;
                }
                if (!isEmpty(wl.id) && isEmpty(wl.count)) {
                    alert("请填写第" + i + "个物料的数量");
                    isOk = false;
                    return isOk;
                }
                idList[idList.length] = wl.id;
                priceList[priceList.length] = wl.price;
                countList[countList.length] = wl.count;
                dwList[dwList.length] = wl.dw;
            }
        });
        if (!isOk)
            return false;
        $("#hidWLID").val(idList);
        $("#hidPrices").val(priceList);
        $("#hidCounts").val(countList);
        $("#hidDWBH").val(dwList);
        if (isEmpty($("#hidWLID").val())) {
            alert("至少必须添加一个物料明细");
            return false;
        }
        return true;
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td class=tbHead_L></td>
        <td class=tbHead_M><div style="text-align:center; font-size:16px"><b><asp:Literal ID="liTitle" runat=server>新增</asp:Literal>采购单</b></div></td>
        <td class=tbHead_R></td>
    </tr>
    <tr>
        <td class=tbBody_L></td>
        <td class=tbBody_M>
            <ul class=tags style="width:95%;"><li class=liTitle><div>详细信息</div></li></ul>
            <table class="grvCSS" cellspacing="0" cellpadding=2 border="1" style="width:95%;border-collapse:collapse;">
                <tr>
                    <td width=10% style="text-align:right">供应商：</td>
                    <td style="text-align:left;width:23%"><asp:TextBox ID="txtGYSJC" runat="server" CssClass="textbox" ReadOnly=true/>
                    <input type=button value="选择" class="btn_s" onclick="selGYS()" /><input type=hidden runat=server id="hidGYSID" /></td>                                            
                    <td style="text-align:right">采购日期：</td>
                    <td style="text-align:left"><asp:TextBox ID="txtRQ" runat="server" CssClass="textbox Wdate" Width=90px onclick="WdatePicker({dateFmt: 'yyyy-MM-dd'});"/></td>
                    <td style="text-align:right">计划到货日期：</td>
                    <td style="text-align:left"><asp:TextBox ID="txtDHSJ" runat="server" CssClass="textbox Wdate" Width=90px onclick="WdatePicker({dateFmt: 'yyyy-MM-dd'});"/></td>

                </tr>
                <tr>
                    <td width=10% style="text-align:right">采购部门：</td>
                    <td style="text-align:left; width:30%" colspan=1><asp:DropDownList ID="ddlDepartments" runat="server" CssClass="dropdown" onchange="BindStaff('ddlRSR',this.value,'请选择')"/></td>
                    <td width=10% style="text-align:right">采购人员：</td>
                    <td style="text-align:left;width:24%"><asp:DropDownList ID="ddlRSR" runat="server" CssClass="dropdown" Width="150px" onchange="setStaff('hidStaff',this.value)"></asp:DropDownList>
                    <input type="hidden" runat=server id="hidStaff" /></td>
                </tr>
            </table>
            <ul class=tags style="width:95%;">
                <li class=liTitle><div>明细列表</div></li>
                <li class=liRight><img onclick="selectWL()" title="增加产品明细" src="../Images/Common/btn_add0.png" style="cursor:pointer" /></li>
            </ul>
            <table id="tbGoodsInfo" class=grvCSS cellpadding=0 cellspacing=0 border=1 style="width:95%;border-collapse:collapse;">
                <tr>
                    <th>序号</th><th>物料编号</th><th>物料名称</th><th>物料规格</th><th>颜色</th><th>库存单位</th><th>最新单价</th><th>数量</th><th width=100px></th>
                </tr>            
            <asp:Repeater ID="repMXList" runat="server">
            <ItemTemplate>
                <tr>
                    <td><%# Container.ItemIndex + 1%></td>
                    <td><%# Eval("BH") %><input type=hidden id="hidWid" value='<%# Eval("WPID") %>' /></td>
                    <td><%# Eval("MC") %></td><td><%# Eval("GG") %></td><td><%# Eval("YS") %></td><td><%# Eval("DWMC")%><input type=hidden id='hidDW' value='<%# Eval("DWBH")%>' /></td>
                    <td><input type=text class="textbox" id="txtPrice" value='<%# Eval("DJ") %>' style="width:60px; text-align:center" onchange="setPrice(this)" /></td>
                    <td><input type=text class="textbox" id="txtCount" value='<%# Eval("SL") %>' style="width:60px; text-align:center" onchange="setCount(this)" /></td>
                    <td><img src="../Images/Common/btn_del0.png" onclick="deleteWL(this)" style="cursor:pointer" /></td>
                </tr>
            </ItemTemplate>
            </asp:Repeater>
            </table>
            <div id="divTotalMoney" style="text-align:left; display:none; width:95%; margin:5px 0px">
                总计：<span id='spanMoney' style="color:Red"></span> <span id='spanDW'></span>
            </div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <p><asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/Images/Common/btn_save0.png" onclick="btnSave_Click" OnClientClick="return checkSubmit();" />&nbsp;&nbsp;
               <a href="CGD_Query.aspx?lx=0"><img src="../Images/Common/btn_back0.png" border=0 /></a>&nbsp;&nbsp;
               <asp:Label ID="labResult" runat="server" ForeColor=Red></asp:Label>
            </p></ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
            </Triggers>
            </asp:UpdatePanel>
        </td>
        <td class=tbBody_R></td>
    </tr>
    <tr>
        <td class=tbFoot_L></td>
        <td class=tbFoot_M></td>
        <td class=tbFoot_R></td>
    </tr>
</table>
<asp:HiddenField ID="hidWLID" runat="server" /><asp:HiddenField ID="hidPrices" runat="server" /><asp:HiddenField ID="hidDWBH" runat="server" />
<asp:HiddenField ID="hidCounts" runat="server" />
</asp:Content>
